{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Discovered device  54:6c:0e:b1:5d:73\n",
      "Discovered device  54:6c:0e:b1:5d:5e\n",
      "Discovered device  63:66:11:7b:96:71\n",
      "Discovered device  26:89:6a:30:2a:03\n",
      "Discovered device  33:b5:34:68:c3:21\n",
      "Discovered device  54:6c:0e:b1:5e:c0\n",
      "Discovered device  3a:fc:f4:2a:f3:d3\n",
      "Discovered device  54:6c:0e:b1:58:3f\n",
      "Discovered device  14:ef:28:e2:29:6a\n",
      "Discovered device  47:40:e7:2b:7d:72\n",
      "Discovered device  1e:73:81:a6:33:44\n",
      "Received new data from 54:6c:0e:b1:58:3f\n",
      "Received new data from 54:6c:0e:b1:5d:73\n",
      "Received new data from 54:6c:0e:b1:5d:5e\n",
      "Discovered device  54:6c:0e:b1:5e:de\n",
      "Discovered device  44:81:2d:4a:b5:72\n",
      "Discovered device  39:d9:d0:d5:bd:2b\n",
      "Received new data from 54:6c:0e:b1:5e:c0\n",
      "Discovered device  58:2d:34:30:36:d1\n",
      "Discovered device  2c:41:a1:98:74:f5\n",
      "Device 54:6c:0e:b1:58:3f (public), RSSI=-46 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 0000180d-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = HRScope\n",
      "Device 39:d9:d0:d5:bd:2b (random), RSSI=-73 dB\n",
      "  Manufacturer = 060001092002efa2f3d50bc65257bf1006842a5aeb158c82eab728d2b6\n",
      "Device 54:6c:0e:b1:5d:73 (public), RSSI=-55 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 2c:41:a1:98:74:f5 (public), RSSI=-95 dB\n",
      "  Flags = 1a\n",
      "  Complete 16b Services = 0000febe-0000-1000-8000-00805f9b34fb\n",
      "  Manufacturer = 100340110130\n",
      "Device 1e:73:81:a6:33:44 (random), RSSI=-50 dB\n",
      "  Manufacturer = 060001092002970db66937dca9fa5c299096186f793c2941156764d64b\n",
      "Device 58:2d:34:30:36:d1 (public), RSSI=-54 dB\n",
      "  Flags = 06\n",
      "  Complete 16b Services = 0000180f-0000-1000-8000-00805f9b34fb,0000180a-0000-1000-8000-00805f9b34fb\n",
      "  16b Service Data = ffffd772eba0ceec\n",
      "  Complete Local Name = MJ_HT_V1\n",
      "Device 33:b5:34:68:c3:21 (random), RSSI=-83 dB\n",
      "  Manufacturer = 06000109200278f38e4efa75e16d2eb8d142692f9b1ba95e5cc50dcb83\n",
      "Device 54:6c:0e:b1:5e:de (public), RSSI=-24 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 26:89:6a:30:2a:03 (random), RSSI=-91 dB\n",
      "  Manufacturer = 060001092002d711cf6bcfea2c977733dd3e3f437d8b3b122dad9049c1\n",
      "Device 14:ef:28:e2:29:6a (random), RSSI=-67 dB\n",
      "  Manufacturer = 0600010920023b4ac909f3bf20f8e88b34b19fb74af4dd20dadf8df396\n",
      "Device 3a:fc:f4:2a:f3:d3 (random), RSSI=-65 dB\n",
      "  Manufacturer = 060001092002e6dd2a16b93c99e35f105c8c9ece7f27619fb573086614\n",
      "Device 44:81:2d:4a:b5:72 (random), RSSI=-88 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c0010050b1c69aa6a\n",
      "Device 54:6c:0e:b1:5e:c0 (public), RSSI=-57 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 54:6c:0e:b1:5d:5e (public), RSSI=-73 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 63:66:11:7b:96:71 (random), RSSI=-84 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c001005031c459c4d\n",
      "Device 47:40:e7:2b:7d:72 (random), RSSI=-73 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c001005031c1cb5c7\n"
     ]
    }
   ],
   "source": [
    "from bluepy.btle import Scanner, DefaultDelegate\n",
    "\n",
    "class ScanDelegate(DefaultDelegate):\n",
    "    def __init__(self):\n",
    "        DefaultDelegate.__init__(self)\n",
    "\n",
    "    def handleDiscovery(self, dev, isNewDev, isNewData):\n",
    "        if isNewDev:\n",
    "            print(\"Discovered device \",  dev.addr)\n",
    "        elif isNewData:\n",
    "            print(\"Received new data from\", dev.addr)\n",
    "\n",
    "scanner = Scanner().withDelegate(ScanDelegate())\n",
    "devices = scanner.scan(10.0)\n",
    "\n",
    "for dev in devices:\n",
    "    print(\"Device %s (%s), RSSI=%d dB\" % (dev.addr, dev.addrType, dev.rssi) )\n",
    "    for (adtype, desc, value) in dev.getScanData():\n",
    "        print(\"  %s = %s\" % (desc, value) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x00'\n",
      "b'\\x00\\x00'\n",
      "b'\\x00'\n",
      "b'\\x01\\x00'\n",
      "32 b'\\x17\\xc8\\x00\\x00\\x03\\x00\\x00\\x00'\n",
      "23 200 0 0 None b'\\x03\\x00\\x00\\x00'\n",
      "32 b'\\x17\\xc8\\x00\\x00\\x04\\x00\\x00\\x00'\n",
      "23 200 0 0 None b'\\x04\\x00\\x00\\x00'\n",
      "32 b'\\x17\\xc8\\x00\\x00\\x05\\x00\\x00\\x00'\n",
      "23 200 0 0 None b'\\x05\\x00\\x00\\x00'\n",
      "32 b'\\x17\\xc8\\x00\\x00\\x06\\x00\\x00\\x00'\n",
      "23 200 0 0 None b'\\x06\\x00\\x00\\x00'\n",
      "32 b'\\x17\\xc8\\x00\\x00\\x07\\x00\\x00\\x00'\n",
      "23 200 0 0 None b'\\x07\\x00\\x00\\x00'\n"
     ]
    }
   ],
   "source": [
    "from bluepy import btle\n",
    "import re\n",
    "import time\n",
    "class EEGBand(object):\n",
    "    def __init__(self):\n",
    "        self.quality = None\n",
    "        self.attention = None\n",
    "        self.meditation = None\n",
    "        self.blinkStrength = None\n",
    "        self.timestamp = None\n",
    "        self.received = False\n",
    "        self.count = 0\n",
    "\n",
    "    def handleNotification(self, cHandle, data):\n",
    "        print(cHandle,data)\n",
    "        \n",
    "        if cHandle == 32:\n",
    "            index = 1\n",
    "            flag = data[0]\n",
    "            if flag & 1 == 1:\n",
    "                self.quality = data[index]\n",
    "                index = index + 1\n",
    "            if flag & 2 == 2:\n",
    "                self.attention = data[index]\n",
    "                index = index + 1\n",
    "            if flag & 4 == 4:\n",
    "                self.meditation = data[index]\n",
    "                index = index + 1\n",
    "            if flag & 8 == 6:\n",
    "                self.blinkStrength = data[index]\n",
    "                index = index + 1\n",
    "            if flag & 16 == 16:\n",
    "                self.timestamp = data[index:]\n",
    "                index = index + 1\n",
    "            print(flag,self.quality,self.attention,self.meditation,self.blinkStrength,self.timestamp)\n",
    "            self.count = self.count + 1\n",
    "            if(self.count > 4): self.received = True\n",
    "def get_data():\n",
    "    address = \"54:6c:0e:b1:5e:de\"\n",
    "    p = btle.Peripheral(address)\n",
    "    delegate = EEGBand()\n",
    "    p.withDelegate( delegate )\n",
    "    print(p.readCharacteristic(0x20))  \n",
    "    print(p.readCharacteristic(0x21))\n",
    "    p.writeCharacteristic(0x21, bytearray([1, 0]), True)\n",
    "    print(p.readCharacteristic(0x20))    \n",
    "    print(p.readCharacteristic(0x21))\n",
    "#     p.writeCharacteristic(0x25, bytearray([1, 0]), True)\n",
    "#     p.writeCharacteristic(0x29, bytearray([1, 0]), True)\n",
    "\n",
    "    while not delegate.received:\n",
    "        p.waitForNotifications(5.0)\n",
    "    p.disconnect()\n",
    "    return\n",
    "    temperature = delegate.temperature\n",
    "    humidity = delegate.humidity\n",
    "    print(\"温度：\",temperature,\"  湿度：\",humidity,\"%\")\n",
    "get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
